package search_res_tab

import (
	"file_explorer/common/log"
	"file_explorer/view/packed_widgets"
	"file_explorer/view/store"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
)

type Toolbar struct {
	*fyne.Container

	addOpListButton *packed_widgets.Button // 添加操作列表按钮

	tabCtx *store.SearchResTabContext
}

func (toolbar *Toolbar) refreshData() {
	// 编辑元数据、打开url按钮 显示
	if len(toolbar.tabCtx.CheckedList) > 0 {
		toolbar.getAddOpListButton().Enable()
	} else {
		toolbar.getAddOpListButton().Disable()
	}
}

// 获取添加操作列表按钮
func (toolbar *Toolbar) getAddOpListButton() *packed_widgets.Button {
	if toolbar.addOpListButton != nil {
		return toolbar.addOpListButton
	}

	toolbar.addOpListButton = packed_widgets.NewButton("批量操作", theme.ContentPasteIcon(), func() {
		log.Info("添加%d项到操作列表", len(toolbar.tabCtx.CheckedList))
		toolbar.tabCtx.GetGlobalContext().OpListContext.AddFileInfos(toolbar.tabCtx.CheckedList)
	})

	return toolbar.addOpListButton
}

func NewToolbar(tabCtx *store.SearchResTabContext, searchResTab *SearchResTab) *Toolbar {
	toolbar := &Toolbar{
		tabCtx: tabCtx,
	}

	toolbar.Container = container.NewVBox(
		widget.NewToolbar(
			toolbar.getAddOpListButton(),
			widget.NewToolbarSpacer(),
			searchResTab.FileList.Checklist.GetReverseButton(),
			searchResTab.FileList.Checklist.GetRangeButton(),
		),
	)

	return toolbar
}
